package com.ejlchina.tool.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.GridLayoutManager
import com.adolf.androiddemo.R
import com.adolf.androiddemo.databinding.FragmentDebugBinding
import com.ejlchina.tool.MainActivity
import com.huyuhui.fastble.data.BleDevice
import com.ejlchina.tool.adapter.DebugPageFunctionAdapter
import com.ejlchina.tool.base.BaseBindingFragment2
import com.ejlchina.tool.bean.FunctionBean
import com.ejlchina.tool.constant.DEVICE_MODE_ONLINE
import com.ejlchina.tool.extension.safeNavigate
import com.ejlchina.tool.network.NetApi
import com.ejlchina.tool.util.showToast
import com.ejlchina.tool.view.GridItemDecoration


class DebugFragment : BaseBindingFragment2<FragmentDebugBinding>() {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
        initData()
    }

    override fun getViewBinding(inflater: LayoutInflater, container: ViewGroup): FragmentDebugBinding {
        return FragmentDebugBinding.inflate(inflater,container,true)
    }

private fun initView(){
    setHeadVisibility(View.GONE)
    initListener()
    setHeadText()
}
    private fun setHeadText(){
        binding.includeHead.tvAppTitle.text =
        if (checkDeviceMode() == DEVICE_MODE_ONLINE) getString(R.string.device_nickname_online) else getString(R.string.device_nickname_offline)
    }
    private fun initData(){
        if (checkDeviceMode() == DEVICE_MODE_ONLINE){
            initFunctionData()
        }else {
            initFunctionDataOffline()
        }
        setConnectCount()
    }

    override fun whenDeviceDisConnect(disConnectDevice: BleDevice) {
        super.whenDeviceDisConnect(disConnectDevice)
        setConnectCount()
    }

    private fun initListener(){
        binding.includeHead.ivMenu.setOnClickListener {
            (requireActivity() as MainActivity).openDraw()
        }

        binding.includeHead.tvConnectCount.setOnClickListener {
            if (checkDeviceMode() == DEVICE_MODE_ONLINE){
                findNavController().safeNavigate(R.id.action_debug_to_scan_fragment)
            } else {
                findNavController().safeNavigate(R.id.action_debug_to_scan_offline_fragment)
            }
        }
    }

    private fun setConnectCount(){
        if (isAdded){
            val count = viewModelActivity.getAllConnectDevices().size
            binding.includeHead.tvConnectCount.text = "$count"
        }
    }


    private fun initFunctionData() {
        val functionList = mutableListOf(
            FunctionBean(1,R.drawable.icon_function_push_rod, R.string.function_push) { findNavController().safeNavigate(R.id.action_debug_to_push_rod_parameters_fragment)},
            FunctionBean(2,R.drawable.icon_function_input,R.string.function_input){findNavController().safeNavigate(R.id.action_debug_to_input_signal_fragment)},
            FunctionBean(3,R.drawable.icon_function_prot,R.string.function_port){findNavController().safeNavigate(R.id.action_debug_to_extend_serial_port)},
            FunctionBean(4,R.drawable.icon_function_temperature,R.string.function_temperature){findNavController().safeNavigate(R.id.action_debug_to_temperature_alarm)},
            FunctionBean(5,R.drawable.icon_function_door_machine,R.string.function_door_machine){findNavController().safeNavigate(R.id.action_debug_to_door_control)},
            FunctionBean(6,R.drawable.icon_function_full,R.string.function_full){findNavController().safeNavigate(R.id.action_debug_to_full_and_distance)},
            FunctionBean(7,R.drawable.icon_function_weight,R.string.function_weight){findNavController().safeNavigate(R.id.weightCalibrationFragment)},
            FunctionBean(8,R.drawable.icon_function_light_volumn,R.string.function_light_volume){findNavController().safeNavigate(R.id.action_debug_to_light_and_volume)},
            FunctionBean(9,R.drawable.icon_function_reuse,R.string.function_multiplex){findNavController().safeNavigate(R.id.action_debug_to_reuse_function)},
            FunctionBean(10,R.drawable.icon_function_firmware_upgrade,R.string.function_upgrade){ findNavController().safeNavigate(R.id.action_debug_to_firmware_upgrade_fragment)},
            FunctionBean(11,R.drawable.icon_function_scan_card,R.string.function_scan_code){findNavController().safeNavigate(R.id.action_debug_to_scan_card)},
            FunctionBean(12,R.drawable.icon_index_fix,R.string.function_index_fix){findNavController().safeNavigate(R.id.action_debug_to_index_fix)}
        )

        binding.rvFunction.apply {
            adapter = DebugPageFunctionAdapter(functionList){
//                if (it.index != 12){
//                    checkIsAllDeviceFixed{
//                        it.action.invoke()
//                    }
//                }else {
                    it.action.invoke()
//                }

            }
            layoutManager = GridLayoutManager(requireContext(),4)
            if (itemDecorationCount == 0){
                addItemDecoration(GridItemDecoration(4,20,20))
            }
        }
    }

    private fun initFunctionDataOffline() {
        val functionList = mutableListOf(
            FunctionBean(1,R.drawable.icon_function_push_rod, R.string.function_push) { findNavController().safeNavigate(R.id.action_debug_to_push_rod_parameters_offline_fragment)},
            FunctionBean(2,R.drawable.icon_function_input,R.string.function_input){findNavController().safeNavigate(R.id.action_debug_to_input_signal_offline_fragment)},
            FunctionBean(4,R.drawable.icon_function_temperature,R.string.function_temperature){findNavController().safeNavigate(R.id.action_debug_to_temperature_alarm_offline)},
            FunctionBean(8,R.drawable.icon_function_light_volumn,R.string.function_volume_setting){findNavController().safeNavigate(R.id.action_debug_to_volume_offline)},
            FunctionBean(10,R.drawable.icon_function_firmware_upgrade,R.string.function_upgrade){findNavController().safeNavigate(R.id.action_debug_to_firmware_upgrade_fragment)}
        )

        binding.rvFunction.apply {
            adapter = DebugPageFunctionAdapter(functionList){
                it.action.invoke()
            }
            layoutManager = GridLayoutManager(requireContext(),4)
            if (itemDecorationCount == 0){
                addItemDecoration(GridItemDecoration(4,20,20))
            }
        }
    }

    private fun checkIsAllDeviceFixed(callback: () -> Unit){
        showLoading()
        NetApi.checkIsAllDeviceIndexed(viewModelActivity.getAllDeviceMacString()){ checkResultBean ->
            dismissLoading()
            checkResultBean?.let {
                if (it.status){
                    callback()
                }else {
                    showToast(it.errMsg)
                }
            }?: run{
                showToast("请求异常")
            }
        }
    }

    override fun initOnCreateView() {
        super.initOnCreateView()
        setActionBarPadding(false)
    }

    override fun onResume() {
        super.onResume()
        setConnectCount()
    }
}